package com.example.springblog2.controller;

import com.example.springblog2.model.Result;
import com.example.springblog2.model.UserInfo;
import com.example.springblog2.service.BlogService;
import com.example.springblog2.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import utils.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;

    @RequestMapping("/login")
    public Result login(String name,String password) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
            return null;
        }
        UserInfo userInfo = userService.queryByName(name);
        if (userInfo == null || userInfo.getId() < 0) {
            return Result.fail("用户不存在");
        }
        if (password.equals(userInfo.getPassword())) {
            Map<String,Object> claim = new HashMap<>();
            claim.put("name",userInfo.getUserName());
            claim.put("id",userInfo.getId());
            String token = JwtUtil.genJwt(claim);
            return Result.success(token);
        }else {
            return Result.fail("密码错误");
        }
    }

    @RequestMapping("/getUserInfo")
    public UserInfo getUserInfo(HttpServletRequest request) {
        String token = request.getHeader("user_token");
        Integer userId = JwtUtil.getUserIdFromToken(token);
        if (userId == null || userId < 0) return null;
        UserInfo userInfo = userService.queryById(userId);
        userInfo.setPassword("");
        return userInfo;
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId) {
        Integer userId = blogService.getUserIdByBlogId(blogId);
        if (userId == null || userId < 0) return null;
        return userService.queryById(userId);
    }

    @RequestMapping("/isAuthor")
    public Boolean isAuthor(Integer blogId,HttpServletRequest request) {
        Integer authorId = blogService.getUserIdByBlogId(blogId);
        String token = request.getHeader("user_token");
        Integer userId = JwtUtil.getUserIdFromToken(token);
        if (authorId == null || userId == null) return false;
        if (authorId.equals(userId)) return true;
        return false;
    }

    @RequestMapping("/registry")
    public Result registry(String name,String password,String repassword) {
        if (!StringUtils.hasLength(name) || ! StringUtils.hasLength(password)) return Result.fail("您输入的账号或者密码为空",-1);
        //对于注册账号来说，首先需要判断该用户是否已经存在
        UserInfo userInfo = userService.queryByName(name);
        if (userInfo != null) return Result.fail("此账号已存在",0);
        //当用户不存在时，就需要判断用户名和密码的合法性了
        //这里规定，用户名的长度需大于等于4，且长度不长于11位，密码的长度至少为6位，且长度不长于20位
        if (name.length() < 4 || name.length() > 11) return Result.fail("请输入符合规范的用户名，范围[4,11]",-1);
        //判断两次输入的密码是否相同
        if (!password.equals(repassword)) return Result.fail("两次输入的密码不相同，请确定后重试",-1);
        if (password.length() < 6 || password.length() > 20) return Result.fail("请输入符合规范的密码,范围[6,20]",-1);
        //当判断完成用户名和密码的合法性之后就将该数据存入数据库中
        Integer ret = userService.registry(name,password);
        if (ret > 0) return Result.success(1);
        else return Result.fail("注册失败，请联系管理员",-2);
    }
}
